package com.wsoft.hr.pm.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.HrPmRedisKeyConstant;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.BaseEntity;
import com.wsoft.hr.pm.entity.HrPmGradeMemberEntity;
import com.wsoft.hr.pm.mapper.HrPmGradeMemberMapper;
import com.wsoft.hr.pm.query.grade.member.HrPmGradeMemberListQuery;
import com.wsoft.hr.pm.query.grade.member.HrPmGradeRemoveMemberListQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author Z001
 * @Date 2025-03-01 08:35:23
 */
@Service
public class HrPmGradeMemberManager extends ServiceImpl<HrPmGradeMemberMapper, HrPmGradeMemberEntity> {

    @Resource
    private RedisService redisService;


    /**
     * 查询列表
     * @param param
     * @return
     */
    public List<HrPmGradeMemberEntity> list(HrPmGradeMemberListQuery param){
        QueryWrapper<HrPmGradeMemberEntity> query = new QueryWrapper<>();
        //搜索参数
        query.lambda().eq(null != param.getGradeId(),HrPmGradeMemberEntity::getGradeId,param.getGradeId())
                .eq(null != param.getHrOrgId(),HrPmGradeMemberEntity::getHrOrgId,param.getHrOrgId())
                .eq(null != param.getGradeOrgId(),HrPmGradeMemberEntity::getGradeOrgId,param.getGradeOrgId())
                .ne(HrPmGradeMemberEntity::getIsRemove,1);
        if(StrUtil.isNotBlank(param.getMemberCodeOrName())){
            query.lambda().like(HrPmGradeMemberEntity::getMemberName,param.getMemberCodeOrName());
        }
        return super.list(query);
    }

    /**
     * 跟新用户等级
     * @param gradeId
     * @param memberId
     * @param grade
     * @return
     */
    public boolean batchUpdateGrade(Long gradeId,Long memberId,String grade){
        HrPmGradeMemberEntity memberEntity = new HrPmGradeMemberEntity();
        memberEntity.setGrade(grade);
        QueryWrapper<HrPmGradeMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(HrPmGradeMemberEntity::getGradeId,gradeId)
                .eq(HrPmGradeMemberEntity::getMemberId,memberId);
        return super.update(memberEntity,query);
    }


    public List<Long> getIds(Long gradeId,Long memberId,Integer isRemove){
        QueryWrapper<HrPmGradeMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(HrPmGradeMemberEntity::getGradeId,gradeId)
                .eq(HrPmGradeMemberEntity::getMemberId,memberId)
                .eq(HrPmGradeMemberEntity::getIsRemove,isRemove);
        List<HrPmGradeMemberEntity> list = list(query);
        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        return list.stream().map(BaseEntity::getId).collect(Collectors.toList());

    }
    /**
     * 修改是否移除
     * @param remove
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateIsRemove(boolean remove,List<Long> id){
        List<HrPmGradeMemberEntity> list = listByIds(id);
        if(CollUtil.isEmpty(list)){
            return false;
        }
        for (HrPmGradeMemberEntity entity : list) {
            if(remove){
                entity.setIsRemove(1);
            }else{
                entity.setIsRemove(0);
            }
        }
        boolean b = super.updateBatchById(list);
        if(b){
            redisService.deleteByKeyPattern( HrPmRedisKeyConstant.GRADE_STATISTICS + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public HrPmGradeMemberEntity getById(Long id){
        return super.getById(id);
    }

    /**
     * 新增
     * @param entity
     * @return
     */
    @Override
    public boolean save(HrPmGradeMemberEntity entity){
        return super.save(entity);
    }

    /**
     * 更新
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(HrPmGradeMemberEntity entity){
        return super.updateById(entity);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        return super.removeById(id);
    }

    /**
     * 获取移除人员名单
     * @param query
     * @return
     */
    public List<HrPmGradeMemberEntity> removeMemberList(HrPmGradeRemoveMemberListQuery param) {
        QueryWrapper<HrPmGradeMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(HrPmGradeMemberEntity::getIsRemove,1)
                .eq(null != param.getGradeId(),HrPmGradeMemberEntity::getGradeId,param.getGradeId())
                .eq(StrUtil.isNotBlank(param.getMemberCode()),HrPmGradeMemberEntity::getMemberCode,param.getMemberCode())
                .eq(StrUtil.isNotBlank(param.getMemberName()),HrPmGradeMemberEntity::getMemberName,param.getMemberName());
        List<HrPmGradeMemberEntity> list = list(query);

        List<HrPmGradeMemberEntity> resultList = new ArrayList<>();
        Set<Long> hashSet = new HashSet<>();
        for (HrPmGradeMemberEntity memberEntity : list) {
            if(!hashSet.contains(memberEntity.getMemberId())){
                resultList.add(memberEntity);
                hashSet.add(memberEntity.getMemberId());
            }
        }
        return resultList;
    }
}
